Ext.define('MyBlog.controller.EntryTabs', {
	extend: 'Ext.app.Controller',
	
	models:['Entry'],
	
	stores:['Entry', 'EntryList'],
	
	views:['EntryTabs', 'NewEntryForm'],
	
	init: function() {
		console.log('Initialized Users! This happens before the Application launch function is called');
      	this.control({
			"newEntryForm button[action=save]":{
				click: this.saveEntry
			}
		})
    },
	saveEntry: function(button){
		//console.log("Cliked save");
		var form = button.up('form');
		var record = form.getRecord();
		var values = form.getValues();
		
		/*record.set(values);
		
		this.getEntryListStore().sync();*/
		
		form.getForm().submit({
			url: 'data/entries.php',
			params: { action: 'add', v: values },
			success: function (form, action) {
				console.log("entro");
                /*Ext.MessageBox.show({
                    title: 'Success !',
                    msg: 'Asset added successfully<br />',
                    icon: Ext.MessageBox.INFO
                });*/
			
				var data = Ext.decode(action.response.responseText);
				
				var rootNode = Ext.getCmp("entryListTree").getStore().getRootNode();
				rootNode.appendChild({  
					leaf: true,
					title: values.title,
					id: data.id,
					entry: values.entry
				});
				//Ext.getCmp("entryListTree").getView().refresh();
            }
		});
	}
	
});